﻿package com.sff.style {
	import com.sff.core.sff_internal;
	
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	import flash.utils.Proxy;
	
	use namespace sff_internal;
	
	public class CssStyle {
		
		private var strSelector		:String;
		private var dicProperty		:Dictionary;
		private var textFormat		:TextFormat;
		private var styleSheet		:StyleSheet;
				
		public function CssStyle(selector:String) {
			this.strSelector = selector;
			dicProperty = new Dictionary();
		}		
		
		public function get selector():String { 
			return this.strSelector;
		}
		
		public function hasProperty(property:String): Boolean {
			return dicProperty[property] ? true : false;
		}
		
		public function setProperty(property:String, value:Object):void {
			if (property && value) {
				//auto rewrite old property if property exist
				dicProperty[property] = value;
			}
		}
		
		public function getProperty(property:String): Object {
			return dicProperty[property];
		}			
		
		public function format(target:Object, embedFonts:Boolean = true):void {
			//apply TextFormat if TextField
			if (target is TextField) {
				
				TextField(target).defaultTextFormat = textFormat;
				TextField(target).setTextFormat(textFormat);
				
				//autoset "embedFonts" for embedded fonts, can enable embedFonts if there is no fontFamily for object
				if (textFormat.font == '' || textFormat.font == null) { 
					TextField(target).embedFonts = false;
				}	
				
				//test styleSheet of TextField
				//TextField(target).styleSheet = styleSheet;				
				//TextField(target).htmlText = "<span class='." + selector + ">This is long text with multiline</span>";
			}	
			
			//apply property 
			applyProperty(target, embedFonts);
		}
		
		sff_internal function get properties():Dictionary {
			return this.dicProperty;
		}
		
		sff_internal function setStyleSheet(sheet:StyleSheet, object:Object):void {
			styleSheet = sheet;
			
			textFormat = styleSheet.transform(object);
			
			for (var key:String in object) {
				setProperty(key, object[key]);
			}
		}
		
		sff_internal function applyProperty(target:Object, embedFonts:Boolean):void {
			
			var _props:Dictionary = new Dictionary();
			//_props["embedFonts"] = true;
			
			var  l : int;
			var pname : String;
			
			// int - uint
			l = intPropperty.length;
			var pvalInt : int;
			while( --l > -1 ) {
				pname = intPropperty[l];
				if( dicProperty[ pname ] == undefined ) continue;
				
				pvalInt = parseInt( prepInt( dicProperty[ pname ]) );				
				if ( ! isNaN( pvalInt ) ) {
					_props[ pname ] = pvalInt;
				}
			}

			// Number
			l = numPropperty.length;
			var pvalNum : Number;
			while( --l > -1 ) {
				pname = numPropperty[ l ];
				if( dicProperty[ pname ] == undefined ) continue;
				pvalNum = parseFloat( dicProperty[ pname ] );
				if( ! isNaN( pvalNum ) ) _props[ pname ] = pvalNum;
			}

			// Boolean
			l = bolPropperty.length;
			var pvalBool : Boolean;
			while( --l > -1 ) {
				pname = bolPropperty[ l ];
				if( dicProperty[ pname ] == undefined ) continue;
				pvalBool = ( dicProperty[ pname ] == "true" );
				_props[ pname ] = pvalBool;
			}
			
			// String
			l = strPropperty.length;
			var pvalStr : String;
			while( --l > -1 ) {
				pname = strPropperty[ l ];
				if( dicProperty[ pname ] == undefined ) continue;
				pvalStr = String( dicProperty[ pname ] );
				_props[ pname ] = pvalStr;
			}
			
			//special case for embed font to be sure text will be display with default font
			//if (_props['embedFonts'] == true) {
				_props['embedFonts'] = embedFonts;
			//}
			
			
			/*			
			// Filters
			l = _filtersProps.length;
			var filter : BitmapFilter;
			var fP : Array; // filterParams
			while( --l > -1 ) {
				pname = filtersPropperty[ l ];
				if( dicProperty[ pname ] == undefined ) continue;
				
				fP = String( dicProperty[ pname ] ).split(' ');
				
				
				for( var i:int = 0; i< fP.length; i++ ){
					fP[i] = parseFloat( prepInt( fP[i] ) );
				}
				
				switch( pname ) {
					case 'dropShadow' : 
						filter = new DropShadowFilter( fP[0], fP[1], fP[2], fP[3], fP[4], fP[5], 1, 2 );
						break;
				}
				
				if( filter ) {
					if( ! _props[ 'filters' ] ) _props[ 'filters' ] = new Array();
					( _props[ 'filters' ] as Array ).push( filter );
				}
				
				filter = null;
			}
			*/
			
			
			//apply properties
			for (var key:String in _props) {	
				target[ key ] = _props[key];
			}	
		}
		
		private function prepInt( val : String ) : String {
			if( val.charAt( 0 ) == "#" ) {
				val = "0x" + val.substr( 1 );
			}	
			return val;
		}
		
		private static const intPropperty: Array = [
											//int
												"maxChars",
												"scrollH",
												"scrollV",
											//uint
												"borderColor",
												"textColor",
												"backgroundColor"
											];

		private static const numPropperty: Array = [
													"height",
													"width",
													"thickness",
													"sharpness"
												];

		private static const bolPropperty: Array = [
													"border",
													"background",
													"embedFonts",
													"multiline",
													"mouseWheelEnabled",
													"alwaysShowSelection",
													"useRichTextClipboard",
													"wordWrap",
													"condenseWhite",
													"selectable",
													"displayAsPassword"
												];
												
		private static const strPropperty: Array = [
													"text",
													"restrict",
													"gridFitType",
													"type",
													"autoSize",
													"antiAliasType",
													"htmlText",
													'filters'
												];
												
		private static const filtersPropperty: Array = [
													"dropShadow"
												];
	}
}